﻿using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using SMS_Step1.Contracts;

namespace SMS_Step1
{
    public partial class NewOrder : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private const IContainer components = null;

        private DataSet dsOrderDetail;
        private new System.Windows.Forms.DataGridViewTextBoxColumn ProductName;

        public NewOrder()
        {
            InitializeComponent();
            InitDataSet();
            dgvOrderDetail.DataSource = dsOrderDetail;
        }

        private void InitDataSet()
        {
            dsOrderDetail= new DataSet("Order");

            var dtOrderDetail= new DataTable("OrderDetail");
            dtOrderDetail.Columns.Add(new DataColumn("Id", typeof (int)));
            dtOrderDetail.Columns.Add(new DataColumn("ProductName", typeof(string)));
            dtOrderDetail.Columns.Add(new DataColumn("ProductId", typeof(string)));
            dtOrderDetail.Columns.Add(new DataColumn("Barcode", typeof(string)));
            dtOrderDetail.Columns.Add(new DataColumn("Price", typeof(decimal)));
            dtOrderDetail.Columns.Add(new DataColumn("Amount", typeof(int)));
            dtOrderDetail.Columns.Add(new DataColumn("Sum", typeof(decimal), "Price * Amount"));
            dtOrderDetail.Columns["Id"].AutoIncrement = true;
            dtOrderDetail.Columns["Id"].AutoIncrementSeed = 1;
            dtOrderDetail.Columns["Id"].AutoIncrementStep = 1;

            dsOrderDetail.Tables.Add(dtOrderDetail);
        }
        private void AddNewRow(OrderDetail orderDetail)
        {
            DataRow drNew = dsOrderDetail.Tables["OrderDetail"].NewRow();
            drNew["ProductName"] = orderDetail.productName;
            drNew["ProductId"] = orderDetail.productId;
            drNew["Barcode"] = orderDetail.barcode;
            drNew["Price"] = orderDetail.productPrice;
            drNew["Amount"] = orderDetail.amount;

            dsOrderDetail.Tables["OrderDetail"].Rows.Add(drNew);
            dgvOrderDetail.Refresh();

            dgvOrderDetail.DataSource = dsOrderDetail.Tables[0].DefaultView;
        }

        private void BtnAdd_Click(object sender, System.EventArgs e)
        {
            AddNewRow(new OrderDetail
                          {
                              productId = 1,
                              productName = "Omo",
                              amount = 10,
                              barcode = "1112121",
                              orderId = 1,
                              productPrice = 314,
                          });
        }

    }
}

